﻿using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace Ipmce.RemoteVoting.Common.Tools.Wcf.Unity.LifetimeManagers
{
	/// <summary>
	/// Base class for Unity WCF lifetime manager support.
	/// </summary>
	/// <typeparam name="T">IExtensibleObject on which to attach.</typeparam>
	public class UnityWcfExtension<T> :InstancesManager, IExtension<T> where T : IExtensibleObject<T>
	{
		#region IExtension<T> Members

		/// <summary>
		/// Enables an extension object to find out when it has been aggregated. Called when the extension is added to the IExtensibleObject.Extensions property.
		/// </summary>
		/// <param name="owner">The extensible object that aggregates this extension.</param>
		public void Attach(T owner)
		{
		}

		/// <summary>
		/// Enables an object to find out when it is no longer aggregated. Called when an extension is removed from the IExtensibleObject.Extensions property.
		/// </summary>
		/// <param name="owner">The extensible object that aggregates this extension.</param>
		public void Detach(T owner)
		{
			// If we are being detached, let's go ahead and clean up, just in case.
			var keys = new List<Guid>(_instances.Keys);

			for (var i = 0; i < keys.Count; i++)
			{
				RemoveInstance(keys[i]);
			}
		}

		#endregion

		
	}
}